package com.xiaoyu.lanling.feature.friend.controller

import `in`.srain.cube.views.list.CubeRecyclerViewAdapter
import `in`.srain.cube.views.list.CubeRecyclerViewSimpleAdapter
import `in`.srain.cube.views.list.PagedListDataModelWithRequestTag
import android.content.Context
import android.text.TextUtils
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.xiaoyu.base.app.GlobalUI
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.base.view.list.BaseListViewController
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.event.fate.MainFateUserListEvent
import com.xiaoyu.lanling.event.friend.FansToFriendEvent
import com.xiaoyu.lanling.event.friend.FriendListEvent
import com.xiaoyu.lanling.event.friend.FriendToFansEvent
import com.xiaoyu.lanling.event.user.SubscribeUserEvent
import com.xiaoyu.lanling.feature.friend.model.FriendItem
import com.xiaoyu.lanling.feature.friend.viewholder.FriendViewHolder
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class FriendListViewController(context: Context, listData: PagedListDataModelWithRequestTag<FriendItem, Any>?, adapter: CubeRecyclerViewAdapter<FriendItem>, swipeRefreshLayout: SwipeRefreshLayout?, recyclerView: RecyclerView?) : BaseListViewController<FriendItem, Any>(context, listData, adapter, swipeRefreshLayout, recyclerView) {

    companion object {
        fun create(context: Context, listData: PagedListDataModelWithRequestTag<FriendItem, Any>?, swipeRefreshLayout: SwipeRefreshLayout?, recyclerView: RecyclerView?): FriendListViewController {

            val adapter = CubeRecyclerViewSimpleAdapter<FriendItem>()
            adapter.setViewHolderClass(0, null, FriendViewHolder::class.java, 20)

            val controller = FriendListViewController(context, listData, adapter, swipeRefreshLayout, recyclerView)
            AppEventBus.bindContainerAndHandler(context, object : SimpleEventHandler() {

                @Subscribe(threadMode = ThreadMode.MAIN)
                fun onEvent(event: FriendListEvent) {
                    controller.onListEvent(event)
                }

                @Subscribe(threadMode = ThreadMode.MAIN)
                fun onEvent(event: FansToFriendEvent) {
                    listData?.listPageInfo?.dataList?.add(0, event.item)
                    adapter.notifyItemInserted(0)
                    GlobalUI.getInstance().showToast(context.getString(R.string.friend_fans_to_friend_toast))
                }

                @Subscribe(threadMode = ThreadMode.MAIN)
                fun onEvent(event: SubscribeUserEvent) {
                    if (event.subscribe) {
                        return
                    }
                    listData?.listPageInfo?.dataList?.forEachIndexed { index, friendItem ->
                        if (TextUtils.equals(event.uid, friendItem.user.uid)) {
                            listData.listPageInfo?.dataList?.removeAt(index)
                            adapter.notifyItemRemoved(index)
                            FriendToFansEvent(friendItem).post()
                            return@forEachIndexed
                        }
                    }
                }
            })
            return controller
        }
    }
}
